<!--
 * @页面名称:免密登录
 * @描述: 
 * @作者: 
 * @Date:
-->
<template>
  <div>
    <el-result icon="success" title="登录跳转中!" sub-title="请耐心等待...">
      <template #icon>
        <a-icon type="smile" theme="twoTone" />
        <SmileTwoTone />
      </template>
      <template #extra>
        <strong style="color: red">{{ Msg }}</strong>
      </template>
    </el-result>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import { useRoute } from "vue-router";
import { storeToRefs } from "pinia";
import axios from "axios";
import { useRouter } from "vue-router";
import { ElNotification } from "element-plus";
import { HOME_URL } from "@/config";
import { SmileTwoTone } from "@ant-design/icons-vue";
import useCurrentInstance from "@/utils/useCurrentInstance";
import ACrypto from "@/utils/crypto";
import { useGlobalStore } from "@/stores/modules/global";
import { useUserStore } from "@/stores/modules/user";
import { useAppStore } from "@/stores/modules/app";
import { useFondsStore } from "@/stores/modules/fonds";
import { initDynamicRouter } from "@/routers/modules/dynamicRouter";
import { useDictionryStore } from "@/stores/modules/dictionary";
import { useTabsStore } from "@/stores/modules/tabs";
import { useKeepAliveStore } from "@/stores/modules/keepAlive";
import { getAllUserInfo } from "@/api/modules/login";
import { getTimeState } from "@/utils";
import { useTheme } from "@/hooks/useTheme";
import { useScrollbar } from "@/hooks/useScrollbar";

const router = useRouter();
const { proxy } = useCurrentInstance();
const globalStore = useGlobalStore();
const userStore = useUserStore();
const appStore = useAppStore();
const fondsStore = useFondsStore();
const dictionryStore = useDictionryStore();
const tabsStore = useTabsStore();
const keepAliveStore = useKeepAliveStore();
const { setScrollbarConfig, initScrollbar } = useScrollbar();

const {
  layout,
  primary,
  isGrey,
  isWeak,
  asideInverted,
  headerInverted,
  isCollapse,
  breadcrumb,
  breadcrumbIcon,
  tabs,
  tabsIcon,
  footer,
  continuousSubmission,
  fondsRouter,
  showChild,
  pageSize,
  pageSpace,
  showOvertime,
  overtime,
  indexKey,
  lastKey,
  prevKey,
  nextKey
} = storeToRefs(globalStore);

// 路由
const route = useRoute();
// 错误码
const errorCode = ref({
  "Error Password": "密码错误!",
  "User in UserBlackList": "该账户在黑名单中,禁止登录!",
  "Account Has Been Disabled": "该账户由于频繁登录,已锁定,请联系管理员!",
  "No Such User": "该账户不存在!",
  "Validate Failed": "登录失败!"
});
// 错误信息
const Msg = ref("");
onMounted(() => {
  console.log("获取路由参数", route.query.name);
  const name = route.query.name;
  if (name) {
    proxy.$vueAxios.get("/MasterBaseUsers/DataCenterLogin", { loginName: ACrypto.setSS(name) }).then(async (data: any) => {
      console.log("免密登录返回数据", data);
      if (data.Mark) {
        // 存储token
        userStore.setToken(data.Model.U_Token);
        // 存储个人信息
        userStore.setUserInfo(data.Model);

        // 设置个人配置
        if (data.Model.U_InterfaceSettings) {
          const setting = JSON.parse(data.Model.U_InterfaceSettings);
          layout.value = setting.layout;
          primary.value = setting.primary;
          isGrey.value = setting.isGrey;
          asideInverted.value = setting.asideInverted;
          headerInverted.value = setting.headerInverted;
          isCollapse.value = setting.isCollapsibleMenu;
          breadcrumb.value = setting.isCrumbs;
          breadcrumbIcon.value = setting.isBreadcrumbIcon;
          tabs.value = setting.isTabBar;
          tabsIcon.value = setting.isTabBarIcon;
          footer.value = setting.isFooter;
          continuousSubmission.value = setting.isContinuousEmission;
          fondsRouter.value = setting.isFullRoute;
          showChild.value = setting.isDisplayChild;
          pageSpace.value = setting.pageSpacing;
          pageSize.value = setting.loadNum;
          showOvertime.value = setting.showOvertime;
          overtime.value = setting.overtime;
          indexKey.value = setting.indexKey;
          lastKey.value = setting.lastKey;
          prevKey.value = setting.prevKey;
          nextKey.value = setting.nextKey;
          isGrey.value = setting.isGrey;
          isWeak.value = setting.isWeak;
          // 设置滚动条配置
          if (setting.scrollbarConfig) {
            setScrollbarConfig(setting.scrollbarConfig);
          }
          // 设置Splite位置
          if (setting.splitpanesPos) {
            globalStore.setGlobalState("splitpanesPos", setting.splitpanesPos);
          }
        }

        // 设置主题色
        const { changePrimary, changeGreyOrWeak, setAsideTheme, setHeaderTheme } = useTheme();
        changePrimary(primary.value);
        // 初始化滚动条样式
        initScrollbar();
        const Res = await axios.get("/config.json");
        appStore.setHideRouter(Res.data.hideRouter);
        // 存储全宗
        await fondsStore.setFondsData();
        // 2.添加动态路由
        await initDynamicRouter();
        // 存储字典数据
        await dictionryStore.setDictionaryObj();
        // 存储系统参数
        await appStore.setSysConfigData();
        // 获取所有用户数据
        await getAllUserInfo();

        // 3.清空 tabs、keepAlive 数据
        tabsStore.closeMultipleTab();
        keepAliveStore.setKeepAliveName();
        // 4.跳转到首页
        router.push(HOME_URL);
        ElNotification({
          title: getTimeState(),
          message: `欢迎登录 ${name}`,
          type: "success",
          duration: 3000
        });
      } else {
        proxy.$message.warning(data.Msg);
      }
    });
  } else {
    Msg.value = "登录跳转参数缺失,请联系管理员!";
  }
});
</script>

<style scoped></style>
